//
//  QSSelectDateView.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/4/7.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSSelectDateView.h"
#import "BRPickerView.h"

@implementation QSSelectDateView
#pragma mark - Lazy Load
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self configSubView];
    }
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectDateAction:)];
    
    [self addGestureRecognizer:tap];
    return self;
}

- (void)configSubView{
    self.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    [self addSubview:self.leftLabel];
    [self addSubview:self.rightLabel];
    [self addSubview:self.rightImage];
    
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kRealValue(15));
        make.centerY.equalTo(self);
    }];
    [_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(- kRealValue(36));
    }];
    [_rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(- kRealValue(12));
        make.width.offset(kRealValue(9));
        make.height.offset(kRealValue(6));
    }];
    NSString *curruntDate = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970] *1000];
    _rightLabel.text = [NSDate dateWithTimestamp:curruntDate format:@"yyyy-MM"];
    
}
- (UILabel *)leftLabel{
    if (!_leftLabel) {
        _leftLabel = [UILabel labelWithName:@"选择月份" font:[UIFont QSFontOfSize14] textColor:[UIColor QSColorBlack666666] textAlignment:NSTextAlignmentLeft];
    }
    return _leftLabel;
}

- (UILabel *)rightLabel{
    if (!_rightLabel) {
        _rightLabel = [UILabel labelWithName:@"请选择时间" font:[UIFont QSFontOfSize16] textColor:[UIColor QSColorBlack434343] textAlignment:NSTextAlignmentLeft];
    }
    return _rightLabel;
}

- (UIImageView *)rightImage{
    if (!_rightImage) {
        _rightImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_selectpeople"]];
    }
    return _rightImage;
}

#pragma mark - Action
- (void)selectDateAction:(UITapGestureRecognizer *)sender{
    [BRDatePickerView showDatePickerWithTitle:@"请选择查询时间" dateType:BRDatePickerModeYM defaultSelValue:nil resultBlock:^(NSDate *selectDate, NSString *selectValue) {
            NSTimeInterval stamp = [selectDate timeIntervalSince1970];
            NSString *selectTime = [NSString stringWithFormat:@"%ld", (long)stamp *1000];
    //        NSString *curruntDate = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970] *1000];
            self.rightLabel.text = selectValue;
        if (self.didSelectDateFinishBlock) {
            self.didSelectDateFinishBlock(selectTime);
        }            
        }];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
